package com.cfh.web.admin;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.cfh.ahead.Constant;
import com.cfh.domain.ConfigExample;
import com.cfh.service.ConfigService;
import com.cfh.service.ConfigService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class BuyReportAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(BuyReportAction.class);

	/**
	 * Logger for this class
	 */

	private static final long serialVersionUID = 1L;
	private ConfigService configService;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("application/vnd.ms-excel");
		res.setHeader("Content-Disposition", "attachment; filename=\"report_"
				+ System.currentTimeMillis() + ".xls\"");

		Map beans = new HashMap();
		beans.put("PRINT_DATE", Tools.getToday());
		ConfigExample example = new ConfigExample();
		example.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_BUYURL);
		
		List list = configService.selectByExample(example, 0, 9999);
		configService.selectMoreFields(list, Constant.CFG_TYPE_BUYURL);
		
		beans.put("list", list);
		XLSTransformer transformer = new XLSTransformer();
		InputStream in = this.getClass().getResourceAsStream("/excel/buy.xls");
		HSSFWorkbook workbook = transformer.transformXLS(in, beans);
		try {
			workbook.write(res.getOutputStream());
		} catch (IOException e) {
			logger.error("report error:" + e);
			e.printStackTrace();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return null;
	}

	

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}


	public ConfigService getConfigService() {
		return configService;
	}

}
